package com.tfjybj.itoo.teach.provider.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.dmsdbj.itoo.tool.base.entity.YearSemesterEntity;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.TrainingProgramsEntity;
import com.tfjybj.itoo.teach.model.CourseModel;
import com.tfjybj.itoo.teach.model.TrainingProgramsModel;
import com.tfjybj.itoo.teach.provider.service.CourseService;
import com.tfjybj.itoo.teach.provider.service.TrainingProgramsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * TrainingProgramsController
 * trainingPrograms表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Api(tags = {"培养计划接口"})
@RequestMapping(value = "/trainingPrograms")
@RestController
public class TrainingProgramsController {

    @Resource
    private TrainingProgramsService trainingProgramsService;

    @Resource
    private CourseService courseService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model TrainingProgramsModel
     * @return 添加的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody TrainingProgramsModel model) {

        if (StringUtils.isEmpty(model.getSemesterId())) {
            return ItooResult.build(ItooResult.FAIL, "学年学期不能为空");
        }
        if (StringUtils.isEmpty(model.getAcademyId())) {
            return ItooResult.build(ItooResult.FAIL, "学院不能为空");
        }
        if (StringUtils.isEmpty(model.getProfessionId())) {
            return ItooResult.build(ItooResult.FAIL, "专业不能为空");
        }
        if (StringUtils.isEmpty(model.getGrade())) {
            return ItooResult.build(ItooResult.FAIL, "年级不能为空");
        }
        if (StringUtils.isEmpty(model.getCourseId())) {
            return ItooResult.build(ItooResult.FAIL, "课程不能为空");
        }
        if (StringUtils.isEmpty(model.getCourseTypeId())) {
            return ItooResult.build(ItooResult.FAIL, "课程类型不能为空");
        }
        if (StringUtils.isEmpty(model.getCourseCategoryId())) {
            return ItooResult.build(ItooResult.FAIL, "课程类别不能为空");
        }
        if (StringUtils.isEmpty(model.getExaminationFormId())) {
            return ItooResult.build(ItooResult.FAIL, "考核形式不能为空");
        }
        TrainingProgramsEntity trainingProgramsEntity = new TrainingProgramsEntity();
        BeanUtils.copyProperties(model, trainingProgramsEntity);
        trainingProgramsEntity.setId(IdWorker.getIdStr());
        trainingProgramsEntity.setIsArrangement(0);
        String idIsExist = trainingProgramsService.findIdIsExist(trainingProgramsEntity.getSemesterId(), trainingProgramsEntity.getProfessionId(), trainingProgramsEntity.getCourseId(), trainingProgramsEntity.getGrade());
        if (!StringUtils.isEmpty(idIsExist)) {
            return ItooResult.build(ItooResult.FAIL, "该培养计划已存在,不能重复添加！");
        }
        trainingProgramsService.save(trainingProgramsEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        trainingProgramsService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        trainingProgramsService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model TrainingProgramsModel
     * @return 修改后的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id修改trainingPrograms")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody TrainingProgramsModel model) {
        if (StringUtils.isEmpty(model.getCourseId())) {
            return ItooResult.build(ItooResult.FAIL, "课程为空");
        }
        if (StringUtils.isEmpty(model.getProfessionId())) {
            return ItooResult.build(ItooResult.FAIL, "专业为空");
        }
        if (StringUtils.isEmpty(model.getSemesterId())) {
            return ItooResult.build(ItooResult.FAIL, "学年学期为空");
        }
        TrainingProgramsEntity trainingProgramsEntity = new TrainingProgramsEntity();
        BeanUtils.copyProperties(model, trainingProgramsEntity);
        String idIsExist = trainingProgramsService.findIdIsExist(trainingProgramsEntity.getSemesterId(), trainingProgramsEntity.getProfessionId(), trainingProgramsEntity.getCourseId(), trainingProgramsEntity.getGrade());
        if (!StringUtils.isEmpty(idIsExist)) {
            return ItooResult.build(ItooResult.FAIL, "该培养计划已存在,请重新编辑！");
        }
        trainingProgramsService.updateById(trainingProgramsEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找TrainingPrograms
     *
     * @param id 主键id
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        TrainingProgramsEntity trainingProgramsEntity = trainingProgramsService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", trainingProgramsEntity);
    }

    /**
     * 根据课程_id查询trainingPrograms
     *
     * @param courseId 课程_id
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 查询的trainingPrograms
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据课程_id查询培养计划", notes = "分页根据课程_id查询培养计划")
    @GetMapping(value = "/queryTrainingProgramsByCourseId/{pageNo}/{pageSize}")
    public ItooResult queryTrainingProgramsByLikeCourseId(@RequestParam(required = false, defaultValue = "") String courseId,
                                                          @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                                          @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<TrainingProgramsEntity> trainingProgramsList = trainingProgramsService.queryByCourseId(courseId, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", trainingProgramsList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据条件模糊分页查询培养计划--培养计划专用
     *
     * @param academyId    学院ID
     * @param professionId 专业ID
     * @param grade        年级
     * @param pageNo       页码
     * @param pageSize     每页条数
     * @return 培养计划实体集合
     * @author 李建帅
     * @since 2.0.0 2018-11-16 10:11:53
     */
    @ApiOperation(value = "按条件模糊分页查询培养计划--培养计划专用")
    @GetMapping(value = "/queryTrainingProgramByProIdListAndGradList/{pageNo}/{pageSize}")
    public ItooResult queryTrainingProgramByProIdListAndGradList(
            @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
            @ApiParam(name = "pageSize", value = "每页条数", required = true, example = "10") @PathVariable Integer pageSize,
            @ApiParam(name = "academyId", value = "学院ID") @RequestParam(required = false, defaultValue = "") String academyId,
            @ApiParam(name = "professionId", value = "专业ID") @RequestParam(required = false, defaultValue = "") String professionId,
            @ApiParam(name = "grade", value = "年级") @RequestParam(required = false, defaultValue = "") String grade
    ) {
        PageInfo<TrainingProgramsModel> trainingProgramsModelList = trainingProgramsService.queryTrainingProgramByProIdListAndGradList(academyId, professionId, grade, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询培养计划成功！", trainingProgramsModelList);
    }

    /**
     * 根据课程类型ID查询课程信息（课程名称、id、代码、学时、类别、性质）
     *
     * @param courseTypeId 课程类型
     * @return 课程信息实体集合
     * @author 李建帅
     * @since 2.0.0 2018-11-10 15:17:46
     */
    @ApiOperation(value = "根据课程类型查询课程信息（课程名称、id、代码、学时、类别、性质）")
    @GetMapping(value = "/queryCourseByCourseTypeId/{courseTypeId}")
    public ItooResult queryCourseByCourseTypeId(@ApiParam(name = "courseTypeId", value = "课程类型id", required = true) @PathVariable String courseTypeId) {
        List<TrainingProgramsModel> trainingProgramsModels = trainingProgramsService.queryCourseByCourseTypeId(courseTypeId);
        return ItooResult.build(ItooResult.SUCCESS, "查询课程信息成功！", trainingProgramsModels);
    }

    /**
     * 根据课程ID查询专业id，专业名称
     *
     * @param courseId 课程id
     * @return 课程信息实体
     * @author 李建帅
     * @since 2.0.0 2018-11-10 21:34:17
     */
    @ApiOperation(value = "根据课程id查询专业信息（专业名称、id）")
    @GetMapping(value = "/queryProfessionByCourseId/{courseId}")
    public ItooResult queryProfessionByCourseId(@ApiParam(name = "courseId", value = "课程id", required = true) @PathVariable String courseId) {
        List<TrainingProgramsModel> trainingProgramsModels = trainingProgramsService.queryProfessionByCourseId(courseId);
        return ItooResult.build(ItooResult.SUCCESS, "查询专业信息成功！", trainingProgramsModels);
    }

    /**
     * 根据课程名称模糊分页查询培养计划
     *
     * @param strLike  模糊条件（课程名称）
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @return 培养计划实体集合
     * @author 李建帅
     * @since 2.0.0 2018-11-11 16:27:44
     */
    @ApiOperation(value = "根据课程名称模糊分页查询培养计划")
    @GetMapping(value = "/queryTrainingProgramListPageInfo/{pageNo}/{pageSize}")
    public ItooResult queryTrainingProgramListPageInfo(@ApiParam(name = "strLike", value = "模糊条件") @RequestParam(required = false, defaultValue = "") String strLike,
                                                       @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                                       @ApiParam(name = "pageSize", value = "每条页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<TrainingProgramsModel> trainingProgramsModelList = trainingProgramsService.queryTrainingProgramListPageInfo(strLike, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询培养计划成功！", trainingProgramsModelList);
    }

    /**
     * 分页查询所有培养计划
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 李建帅
     * @since 2.0.0 2018-11-12 14:52:50
     */
    @ApiOperation(value = "分页查询所有培养计划")
    @GetMapping(value = {"/queryAllTrainingProgramsPageInfo/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<TrainingProgramsModel> trainingProgramsModelList = trainingProgramsService.queryAllTrainingProgramsPageInfo(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询培养计划成功！", trainingProgramsModelList);
    }

    /**
     * 查询所有年级
     *
     * @return 培养计划实体-年级集合
     * @author 李建帅
     * @since 2.0.0 2018/11/13 19:34
     */
    @ApiOperation(value = "查询所有的年级")
    @GetMapping(value = {"/queryAllGrade"})
    public ItooResult queryAllGrade() {
        List<YearSemesterEntity> yearSemesterEntityList = trainingProgramsService.queryAllGrade();
        return ItooResult.build(ItooResult.SUCCESS, "查询年级成功！", yearSemesterEntityList);
    }

    /**
     * 根据课程ID、专业ID、学期学年ID查询培养计划
     *
     * @param courseId     课程ID
     * @param professionId 专业ID
     * @param semesterId   学年学期ID
     * @return 培养计划实体集合
     * @author 李建帅
     * @since 2.0.0 2018/11/13 20:14
     */
    @ApiOperation(value = "根据课程ID,专业ID,学期学年ID查询培养计划")
    @GetMapping(value = {"/queryTpByCoPrSeId/{courseId}/{professionId}/{semesterId}"})
    public ItooResult queryTpByCoPrSeId(@ApiParam(name = "courseId", value = "课程ID", required = true) @PathVariable String courseId,
                                        @ApiParam(name = "professionId", value = "专业ID", required = true) @PathVariable String professionId,
                                        @ApiParam(name = "semesterId", value = "学期学年ID", required = true) @PathVariable String semesterId) {
        List<TrainingProgramsModel> trainingProgramsModels = trainingProgramsService.queryTpByCoPrSeId(courseId, professionId, semesterId);
        return ItooResult.build(ItooResult.SUCCESS, "查询培养计划成功!", trainingProgramsModels);
    }

    /**
     * 根据培养计划ID查询培养计划详情
     *
     * @param idList 培养计划ID
     * @return 培养计划实体集合
     * @author 李建帅
     * @since 2.0.0 2018-11-15 17:17:03
     */
    @ApiOperation(value = "根据培养计划ID查询培养计划详情")
    @GetMapping(value = "/queryTrainingProgramsById/{idList}")
    public ItooResult queryTrainingProgramsById(@ApiParam(name = "idList", value = "培养计划ID", required = true) @PathVariable List<String> idList) {
        List<TrainingProgramsModel> trainingProgramsModelList = trainingProgramsService.queryTrainingProgramsById(idList);
        return ItooResult.build(ItooResult.SUCCESS, "查询培养计划成功!", trainingProgramsModelList);
    }

    /**
     * 根据学年学期ID和专业ID查询课程信息
     *
     * @param semesterId   学年学期ID
     * @param professionId 专业ID
     * @return 课程信息
     * @author 李建帅
     * @since 2.0.0 2018/11/17 9:09
     */
    @ApiOperation(value = "根据学年学期ID和专业ID查询课程信息")
    @GetMapping(value = "/queryCourseBySemProId/{semesterId}/{professionId}")
    public ItooResult queryCourseBySemProId(@ApiParam(name = "semesterId", value = "学年学期ID", required = true) @PathVariable String semesterId,
                                            @ApiParam(name = "professionId", value = "专业ID", required = true) @PathVariable String professionId) {
        List<TrainingProgramsModel> trainingProgramsModels = trainingProgramsService.queryCourseBySemProId(semesterId, professionId);
        return ItooResult.build(ItooResult.SUCCESS, "查询课程信息成功！", trainingProgramsModels);
    }

    /**
     * 下载培养计划模板，批量导入使用
     *
     * @param response 响应
     * @return 下载模板是否成功
     * @author 李建帅
     * @since 2.0.0 2018-11-15 08:53:53
     */
    @ApiOperation(value = "下载培养计划模板，批量导入使用", notes = "下载培养计划模板，批量导入使用")
    @GetMapping(value = {"/downLoadTemplate"})
    public ItooResult downLoadTemplate(HttpServletResponse response) {
        if (trainingProgramsService.downLoadTemplate(response)) {
            return ItooResult.build(ItooResult.SUCCESS, "下载培养计划模板成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "下载培养计划模板失败");
        }
    }

    /**
     * 批量导入培养计划
     *
     * @param multipartFile 批量导入的文件
     * @param request       请求
     * @param response      响应
     * @return ItooResult 批量导入的结果
     * @author 李建帅
     * @since 2.0.0 2018-11-18 09:05:59
     */
    @ApiOperation(value = "批量导入培养计划", notes = "0001:部分成功；0000:成功；1111:失败")
    @PostMapping(value = {"/importTrainingProgramsByTemplate"})
    public ItooResult importTrainingProgramsByTemplate(@RequestParam(value = "file") MultipartFile multipartFile,
                                                       HttpServletRequest request, HttpServletResponse response) {
        return trainingProgramsService.importTemplate(multipartFile, request, response);
    }

    /**
     * 根据学院ID查询非公选课
     *
     * @param academy 学院id
     * @return coursemodel
     * @author 李建帅
     * @since 2018-11-19 15:50:11
     */
    @ApiOperation(value = "根据学院ID查询所有非公选课")
    @GetMapping(value = "/queryNonPubCourseByAcademy/{academy}")
    public ItooResult queryNonPubCourseByAcademy(@ApiParam(name = "academy", value = "学院ID", required = true) @PathVariable String academy) {
        List<CourseModel> courseModels = courseService.queryNonPubCourseByAcademy(academy);
        return ItooResult.build(ItooResult.SUCCESS, "查询非公选课信息成功！", courseModels);
    }

    /**
     * 根据学年学期ID和专业ID，年级（课程类型为专选课已写死）查询专选课课程ID集合--选课使用
     *
     * @param professionId 专业ID
     * @param grade        年级
     * @return 公选课课程ID集合
     * @author 李建帅
     * @since 2.0.0 2018/11/17 19:18
     */
    @ApiOperation(value = "根据学年、专业id和年级查询课程ID")
    @GetMapping(value = "/queryCourseIdBySemProId/{professionId}/{grade}")
    public ItooResult queryCourseIdBySemProId(@ApiParam(name = "professionId", value = "专业ID", required = true) @PathVariable String professionId,
                                              @ApiParam(name = "grade", value = "年级", required = true) @PathVariable String grade) {
        List<String> strings = trainingProgramsService.queryCourseIdBySemProId(professionId, grade);
        return ItooResult.build(ItooResult.SUCCESS, "查询课程id成功！", strings);
    }

    /**
     * 根据学年学期ID、专业ID、课程ID、年级查询培养计划是否已存在
     *
     * @param semesterId   学年学期ID
     * @param professionId 专业ID
     * @param courseId     课程ID
     * @param grade        年级
     * @return 培养计划ID
     * @author 李建帅
     * @since 2.0.0 2018/11/17 19:19
     */
    @ApiOperation(value = "根据学年学期ID、专业ID、课程ID、年级查询培养计划是否已存在")
    @GetMapping(value = "/findIdIsExist/{semesterId}/{professionId}/{courseId}/{grade}")
    public ItooResult findIdIsExist(@ApiParam(name = "semesterId", value = "学年ID", required = true) @PathVariable String semesterId,
                                    @ApiParam(name = "courseId", value = "课程Id", required = true) @PathVariable String courseId,
                                    @ApiParam(name = "professionId", value = "专业ID", required = true) @PathVariable String professionId,
                                    @ApiParam(name = "grade", value = "年级", required = true) @PathVariable String grade) {
        String idIsExist = trainingProgramsService.findIdIsExist(semesterId, professionId, courseId, grade);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功！", idIsExist);
    }

    /**
     * 根据ID导出培养计划
     *
     * @param response 响应
     * @param idList   培养计划ID
     * @return 导出结果信息
     * @author 李建帅
     * @since 2.0.0 2018-11-21 21:08:12
     */
    @ApiOperation(value = "根据ID导出培养计划")
    @GetMapping(value = {"/exportAll"})
    public ItooResult exportAll(HttpServletResponse response, @RequestParam(required = false, defaultValue = "") List<String> idList) {
        return trainingProgramsService.exportByTpId(response, idList);
    }

    /**
     * 根据条件导出培养计划
     *
     * @param response     响应
     * @param academyId    学院ID
     * @param professionId 专业ID
     * @param grade        年级
     * @param strLike      条件
     * @return 导出结果信息
     * @author 李建帅
     * @since 2.0.0 2018-11-21 21:08:12
     */
    @ApiOperation(value = "根据条件导出培养计划")
    @GetMapping(value = {"/exportByStrLike"})
    public ItooResult exportByStrLike(HttpServletResponse response, @RequestParam(required = false, defaultValue = "") String academyId,
                                      @RequestParam(required = false, defaultValue = "") String professionId,
                                      @RequestParam(required = false, defaultValue = "") String grade,
                                      @RequestParam(required = false, defaultValue = "") String strLike) {
        return trainingProgramsService.exportByStrLike(response, academyId, professionId, grade, strLike);
    }

    /**
     * 导出-导入失败的数据
     *
     * @param response  响应
     * @param errorListId 为成功导入的分组id
     * @return 课程信息model
     * @author 李建帅
     * @since 2018-12-2 08:10:56
     */
    @ApiOperation(value = "导出-导入失败的数据")
    @GetMapping(value = "exportFile/downErrorList/{errorListId}")
    public ItooResult downErrorList(HttpServletResponse response,
                                    @ApiParam(name = "errorListId", value = "errorListId", required = true) @PathVariable String errorListId) {

        if (trainingProgramsService.downErrorList(errorListId,response)){
            return ItooResult.build(ItooResult.SUCCESS,"导出成功");
        } else {
            return ItooResult.build(ItooResult.FAIL,"导出失败");
        }
    }

}

